import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_list.dart';
import 'package:flutter_enjoy_plus/pages/home/components/home_nav.dart';
import 'package:flutter_enjoy_plus/stores/counte.dart';
import 'package:flutter_enjoy_plus/utils/http.dart';
import 'package:flutter_enjoy_plus/utils/logger.dart';
import 'package:flutter_enjoy_plus/utils/toast.dart';
import 'package:provider/provider.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    getNotifyList();
    super.initState();
  }

  List notifyList = [];
  // 获取社区公告
  getNotifyList() async {
    try {
      final res = await http.get('/announcement');
      // 请求状态码 此次http请求成功了 前端 -> 发送请求 -> 后端接受到了
      // 业务状态码 此次业务处理成功了
      // 登录场景 用户名 + 密码 -> 发请求 (路径 + 请求方式等ok) -  代表了请求成功
      // 用户名 和 密码 不一定正确 正确 -> 业务成功 / 错误 -> 业务失败
      if (res['code'] != 10000) return ToastUtil.showError('社区列表加载失败~');
      ToastUtil.showSuccess('社区列表加载成功~');
      setState(() {
        notifyList = res['data'];
      });
    } catch (e) {
      ToastUtil.showError('社区列表加载失败~');
      logger.e('请求出现问题了$e');
    }
  }

  @override
  Widget build(BuildContext context) {

     // 使用Provider.of来访问状态
    final counter = Provider.of<CounterModel>(context, listen: true);
    return Scaffold(
        backgroundColor: const Color.fromARGB(40, 85, 145, 175),
        appBar: AppBar(
          title:  Text('享+社区${counter.count}'),
          backgroundColor: Colors.transparent,
          centerTitle: true,
        ),
        body: ListView(
          // scrollDirection: Axis.vertical,
          padding: const EdgeInsets.all(10),
          children: [
            // 导航条
            HomeNav(),
            // 中间广告图
            ClipRRect(
              borderRadius: BorderRadius.circular(10.0),
              child: Image.asset('assets/images/banner@2x.jpg'),
            ),
            // 社区公告
            HomeList(notifyList: notifyList)
          ],
        ));
  }
}
